<template>
  <div>
    <el-select
      ref="selectTree"
      v-model="form.Id"
      filterable
      placeholder="请选择..."
    >
      <el-option
        :key="form.Id"
        :value="form.Id"
        :label="form.label"
        style="height: auto"
        hidden
      />
      <el-tree
        ref="tree"
        :data="data"
        :props="defaultProps"
        :node-key="defaultProps.value"
        @node-click="handleNodeClick"
        default-expand-all
      />
    </el-select>
  </div>
</template>

<script>
export default {
  data() {
    return {
      data: [
        {
          Id: 1,
          label: '一级 1',
          children: [
            {
              Id: 11,
              label: '二级 1-1',
              children: [
                {
                  Id: 111,
                  label: '三级 1-1-1'
                }
              ]
            }
          ]
        },
        {
          Id: 2,
          label: '一级 2',
          children: [
            {
              Id: 21,
              label: '二级 2-1',
              children: [
                {
                  Id: 211,
                  label: '三级 2-1-1'
                }
              ]
            },
            {
              Id: 22,
              label: '二级 2-2',
              children: [
                {
                  Id: 221,
                  label: '三级 2-2-1'
                }
              ]
            }
          ]
        },
        {
          Id: 3,
          label: '一级 3',
          children: [
            {
              Id: 31,
              label: '二级 3-1',
              children: [
                {
                  Id: 311,
                  label: '三级 3-1-1'
                }
              ]
            },
            {
              Id: 32,
              label: '二级 3-2',
              children: [
                {
                  Id: 321,
                  label: '三级 3-2-1'
                }
              ]
            }
          ]
        }
      ],
      treeList: [],
      defaultProps: {
        value: 'Id',
        label: 'label',
        children: 'children'
      },
      form: {
        Id: '',
        lable: ''
      }
    }
  },
  methods: {
    handleNodeClick(data) {
      this.form.Id = data.Id
      this.form.label = data.label
      this.$refs.selectTree.blur()
    }
  }
}
</script>
